<template>
  <div>
    <the-header />
    <div class="banner">
      <el-card :body-style="{ padding: '11px' }" class="img-card">
        <img
          :src="'http://localhost:8888/upload-images/' + userData.avatar"
          class="image"
        />
      </el-card>
      <el-card class="box-card">
        <div class="text item">
          <h3>用户名:{{ userData.username }}</h3>
        </div>
        <div class="text item">
          <h4>邮箱:{{ userData.email }}</h4>
        </div>
        <el-button
          type="success"
          icon="el-icon-edit"
          @click="dialogVisible = true"
          >修改个人信息</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-edit"
          @click="updatePasswordDialogVisible = true"
          >修改密码</el-button
        >
      </el-card>
      <el-table
        :data="tableData"
        style="width: 100%"
        :row-class-name="tableRowClassName"
      >
        <el-table-column prop="tag" label="歌单名字" width="180">
        </el-table-column>
        <el-table-column prop="user.username" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="song.title" label="地址"> </el-table-column>
      </el-table>
    </div>
    <play-bar />

    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
      <el-upload
        action="s"
        list-type="picture-card"
        accept="image/jpeg,image/png,image/jpg"
        ref="uploadData"
        :on-preview="handlePictureCardPreview"
        :http-request="upload"
        :on-remove="handleRemove"
        :auto-upload="false"
        :multiple="false"
        name="upload"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-form :model="userData">
        <el-form-item>
          <el-input v-model="userData.username"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="userData.email"></el-input>
        </el-form-item>
      </el-form>
      <el-dialog :visible.sync="uploadDialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="uploadDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="uploadDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="updatePasswordDialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="passwordForm"
        status-icon
        :rules="rules"
        ref="passwordForm"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input
            type="password"
            v-model="passwordForm.oldPassword"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="passwordForm.password"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input
            type="password"
            v-model="passwordForm.checkPassword"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import TheHeader from "../components/TheHeader.vue";
import PlayBar from "../components/PlayBar";

export default {
  name: "MyMusic",
  components: {
    TheHeader,
    PlayBar
  },
  created() {
    this.getCollect();
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.passwordForm.checkPassword !== "") {
          this.$refs.passwordForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.passwordForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      userData: [],
      uploadData: [],
      passwordForm: {
        oldPassword: "",
        password: "",
        checkPassword: ""
      },
      rules: {
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPassword: [{ validator: validatePass2, trigger: "blur" }]
      },
      dialogImageUrl: "",
      updatePasswordDialogVisible: false,
      uploadDialogVisible: false,
      dialogVisible: false,
      tableData: []
    };
  },
  methods: {
    getCollect() {
      this.userData = this.$store.state.user;
      console.log(this.$store.state.user);
      this.$http
        .get("collect/" + 5 + "/" + 1, {
          params: {
            "user.id": 5
          }
        })
        .then(response => {
          console.log(response.data);
          this.tableData = response.data.data.items;
        })
        .catch(reason => {
          console.log(reason);
        });
    },
    upload(params) {
      const formData = new FormData();
      formData.append("file", params.file);
      this.$http
        .put("user/", formData)
        .then(response => {
          console.log("上传图片成功");
          console.log(response.data);
          params.onSuccess();
        })
        .catch(reason => {
          console.log(reason);
          params.onError();
        });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    tableRowClassName({ row, rowIndex }) {
      console.log(row);
      if (rowIndex === 1) {
        return "warning-row";
      } else if (rowIndex === 3) {
        return "success-row";
      }
      return "";
    }
  },
  submitForm(formName) {
    this.$refs[formName].validate(valid => {
      if (valid) {
        alert("submit!");
      } else {
        console.log("error submit!!");
        return false;
      }
    });
  },
  resetForm(formName) {
    this.$refs[formName].resetFields();
  }
};
</script>

<style scoped>
.banner {
  width: 80%;
  height: 550px;
  margin: 20px auto;
  display: inline-block;
}

.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 480px;
  display: inline-block;
}
.img-card {
  width: 250px;
  display: inline-block;
}
.image {
  max-width: 100%;
  height: auto;
}
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
